package com.iss.supermarket.TradeUI;

import com.iss.supermarket.util.List.ListModelArray;
import com.iss.supermarket.util.entity.Detailed;
import org.apache.log4j.Logger;
import javax.swing.*;
import java.awt.*;
import java.text.DecimalFormat;
import java.util.ArrayList;

public class TradeList extends JFrame {
    //单例模式
    private static TradeList tradelist;
    //logger记录日志
    private static Logger logger = Logger.getLogger("GoodsList");
    private ListModelArray<Detailed> listModelArray;
    private JList<Detailed> jList;
    private JPanel panelPage = new JPanel();
    private JScrollPane jScrollPanel =new JScrollPane();

    public TradeList(){
        InitUI();
    }

    public void InitUI(){
        Box verticalBox = Box.createVerticalBox();

        jScrollPanel.setPreferredSize(new Dimension(400,600));

        JPanel jPanelTableHeader = new JPanel();

        JLabel labelBarCode = new JLabel("条形码");
        labelBarCode.setPreferredSize(new Dimension(100,20));
        labelBarCode.setFont(new Font("黑体",Font.PLAIN,15));
        jPanelTableHeader.add(labelBarCode);

        JLabel labelBroadName = new JLabel("商品名称");
        labelBroadName.setPreferredSize(new Dimension(100,20));
        labelBroadName.setFont(new Font("黑体",Font.PLAIN,15));
        jPanelTableHeader.add(labelBroadName);

        JLabel labelgdNum = new JLabel("数量");
        labelgdNum.setPreferredSize(new Dimension(100,20));
        labelgdNum.setFont(new Font("黑体",Font.PLAIN,15));
        jPanelTableHeader.add(labelgdNum);

        JLabel labelgdPrice = new JLabel("商品价格");
        labelgdPrice.setPreferredSize(new Dimension(100,20));
        labelgdPrice.setFont(new Font("黑体",Font.PLAIN,15));
        jPanelTableHeader.add(labelgdPrice);

        //创建一个列标题视口,设置其视图然后将此列标题视口添加到滚动窗格
        jScrollPanel.setColumnHeaderView(jPanelTableHeader);

        listModelArray = new ListModelArray<Detailed>();
        listModelArray.setList(new ArrayList<Detailed>());

        //listModelArray为JList的listModel,ListModel存放JList的数据,对于jlist的数据操作,通过对ListModel操作实现
        jList = new JList<Detailed>(listModelArray);

        //创建一个视口（如果有必要）并设置其视图。使用此方法指定将显示在滚动窗格中的滚动组件子级。
        jScrollPanel.setViewportView(jList);
        verticalBox.add(jScrollPanel);
        panelPage.add(verticalBox);
    }

    public void flushList(Detailed detailed){
        int src=0;
        int src1;
        //检测listMOdelArray中是否已经存在该商品
        if(listModelArray.getSize() <= 0){
            //listModelArray为空直接存放
            listModelArray.addList(detailed);
        }else if(isContain(listModelArray,detailed)){
            //listModelArray中存在
            //更新listModelArray中该商品的数量
            for(Detailed item:listModelArray.getList()){
                if(item.getGdBarCode().equals(detailed.getGdBarCode())){
                    src=item.getDtgdNum();
                    src1=detailed.getDtgdNum();
                    src1+=src;
                    item.setDtgdNum(src1);
                }
            }
        }else{
            listModelArray.addList(detailed);
        }

        //jlist的渲染器,自定义jlist显示模式
        jList.setCellRenderer(new ListCellRenderer<Detailed> (){
            @Override
            public Component getListCellRendererComponent(JList<? extends Detailed> list, Detailed value, int index, boolean isSelected, boolean cellHasFocus) {

                JPanel jPanelTableHeader = new JPanel();
                //设置选中或鼠标光标移动到的时候的背景颜色
                if(cellHasFocus){
                    jPanelTableHeader.setBackground(Color.RED);
                }
                else if(isSelected){
                    jPanelTableHeader.setBackground(Color.BLUE);
                }else{
                    jPanelTableHeader.setBackground(Color.white);
                }

                //显示商品信息
                JLabel labelBarCode = new JLabel(value.getGdBarCode());
                labelBarCode.setPreferredSize(new Dimension(100,15));
                labelBarCode.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(labelBarCode);

                JLabel labelBroadName = new JLabel(value.getGdBroadName());
                labelBroadName.setPreferredSize(new Dimension(100,15));
                labelBroadName.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(labelBroadName);

                JLabel labelgdNum = new JLabel(String.valueOf(value.getDtgdNum()));
                labelgdNum.setPreferredSize(new Dimension(100,15));
                labelgdNum.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(labelgdNum);

                JLabel labelgdPrice = new JLabel(value.getDtgdPrice());
                labelgdPrice.setPreferredSize(new Dimension(100,15));
                labelgdPrice.setFont(new Font("黑体",Font.PLAIN,14));
                jPanelTableHeader.add(labelgdPrice);

                return jPanelTableHeader;
            }
        });
    }

    public void Clear(){
        //去除jlist
        //存放数据的listModelArray需要被清除
        ListModelArray listModelArray1 = (ListModelArray)jList.getModel();
        listModelArray1.removeAllElements();
        listModelArray1.clearList();
        //作为UI显示的JList需要重新绘制
        jList.repaint();
        jList.validate();
    }

    public boolean isContain(ListModelArray<Detailed> list,Detailed detailed){
        //检测listModelArray中是否包含该商品
        for(int i=0;i<list.getSize();++i){
            if(list.getElementAt(i).getGdBarCode().equals(detailed.getGdBarCode())){
                return true;
            }
        }
        return false;
    }

    public double getTotalAmount(){
        //获得商品总价
        DecimalFormat df = new DecimalFormat("0.00");
        double totalAmount = 0;
        for(int i=0;i<getListLength();++i){
            totalAmount += Double.parseDouble(listModelArray.getElementAt(i).getDtgdPrice()) * listModelArray.getElementAt(i).getDtgdNum();
        }
        totalAmount = Double.parseDouble(df.format(totalAmount));
        return totalAmount;
    }

    public int getTotalNum(){
        //获得商品总数量
        int totalNum = 0;
        for(int i=0;i<getListLength();++i){
            totalNum += listModelArray.getElementAt(i).getDtgdNum();
        }
        return totalNum;
    }

    public int getListLength(){
        return listModelArray.getSize();
    }

    public JPanel getPanelPage() {
        return panelPage;
    }

    //获得listModelArray(即ListModel)
    public ListModelArray<Detailed> getListModelArray(){
        return this.listModelArray;
    }

    public static synchronized TradeList getTradeList(){
        if(tradelist == null)
            tradelist = new TradeList();
        return tradelist;
    }
}